//
//  CPSHShop.m
//  CardMoveGame
//
//  Created by peac on 2025/11/7.
//

#import "CPSHShop.h"
#import "GlobalStatusView.h"
#import "MdIAPManager.h"
@interface MdshopCell : UICollectionViewCell
@property (nonatomic, strong)  UIImageView *mdbgIview;

@property (nonatomic, strong) UILabel *mdcountLabel;
@property (nonatomic, strong) UILabel *mdpriceLabel;
@end

@implementation MdshopCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
  
         _mdbgIview=[[UIImageView alloc]initWithFrame:self.bounds];
      
        [self.contentView addSubview:_mdbgIview];
        
        // 钻石图标

        // 钻石数量标签
        _mdcountLabel = [[UILabel alloc] init];
        _mdcountLabel.textColor = [UIColor whiteColor];
        _mdcountLabel.textAlignment = NSTextAlignmentLeft;
        _mdcountLabel.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview:_mdcountLabel];
        
        // 价格标签
        _mdpriceLabel = [[UILabel alloc] init];
       
        _mdpriceLabel.textColor = [UIColor whiteColor];
        _mdpriceLabel.textAlignment = NSTextAlignmentCenter;
        _mdpriceLabel.font = [UIFont boldSystemFontOfSize:14];
        [self.contentView addSubview:_mdpriceLabel];
 
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat width = self.contentView.bounds.size.width;
    CGFloat height = self.contentView.bounds.size.height;
    
    
    _mdcountLabel.frame = CGRectMake(5,height*0.5,width-20, 20);
    _mdpriceLabel.frame = CGRectMake(0, height*0.74, width, height/5-5);
    
}


@end
@interface CPSHShop ()
@property(nonatomic,strong)NSArray *mdshopArr;
@end

@implementation CPSHShop

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *drawbgIv=[[UIImageView alloc]initWithFrame:self.view.bounds];
    drawbgIv.contentMode = UIViewContentModeScaleAspectFill;
    drawbgIv.image=[UIImage imageNamed:@"bjshop"];
    [self.view addSubview:drawbgIv];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(98,112);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.view.frame.size.width*0.05, self.view.frame.size.height*0.22,self.view.frame.size.width*0.9,self.view.frame.size.height*0.65) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[MdshopCell class] forCellWithReuseIdentifier:@"MdshopCell"];
    [self.view addSubview:collectionView];
    
    self.mdshopArr=@[@{@"mditemimage":@"召唤券",@"count":@120,@"name":@"",@"price":@"120",@"ismnDiamond":@0},@{@"mditemimage":@"召唤券",@"count":@1200,@"name":@"",@"price":@"1200",@"ismnDiamond":@0},@{@"mditemimage":@"钻石购买",@"count":@60,@"productid":@"com.rtebvtfe.iebdesy.6",@"price":@"0.99"},@{@"mditemimage":@"钻石购买",@"count":@120,@"productid":@"com.rtebvtfe.iebdesy.12",@"price":@"1.99"},@{@"mditemimage":@"钻石购买",@"count":@180,@"productid":@"com.rtebvtfe.iebdesy.18",@"price":@"2.99"},@{@"mditemimage":@"钻石购买",@"count":@300,@"productid":@"com.rtebvtfe.iebdesy.30",@"price":@"4.99"}];
    
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return  self.mdshopArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MdshopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MdshopCell" forIndexPath:indexPath];
    if (indexPath.row<2) {
        cell.mdbgIview.image=[UIImage imageNamed:self.mdshopArr[indexPath.row][@"mditemimage"]];
    }else{
        cell.mdbgIview.image=[UIImage imageNamed:self.mdshopArr[indexPath.row][@"mditemimage"]];
        cell.mdcountLabel.text=[NSString stringWithFormat:@"x%@",self.mdshopArr[indexPath.row][@"count"]];
        cell.mdpriceLabel.text=[NSString stringWithFormat:@"$%@",self.mdshopArr[indexPath.row][@"price"]];
    }
    
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *arr=@[@300,@3000];
    if (indexPath.row<2) {
        if ([[NSUserDefaults standardUserDefaults]integerForKey:@"mddiamond"]<[arr[indexPath.row] intValue]) {
         
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Tips"
                                                                         message:@"Not enough diamond"
                                                                  preferredStyle:UIAlertControllerStyleAlert];
        
            UIAlertAction *restartAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
              
            }];
        
            [alert addAction:restartAction];
            [self presentViewController:alert animated:YES completion:nil];
            
            
        }else{
            
            [[NSUserDefaults standardUserDefaults] setInteger:[[NSUserDefaults standardUserDefaults] integerForKey:@"cjqcount"]+[arr[indexPath.row] intValue]/300 forKey:@"cjqcount"];
            [[NSUserDefaults standardUserDefaults] setInteger:[[NSUserDefaults standardUserDefaults]integerForKey:@"mddiamond"]-[arr[indexPath.row] intValue] forKey:@"mddiamond"];
            [[GlobalStatusView sharedInstance] updateDiamondCount:100];
            
            UIView *showView=[[UIView alloc]initWithFrame:self.view.frame];
            showView.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.7];
            [self.view addSubview:showView];
            UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-self.view.frame.size.height*0.16/2, self.view.frame.size.height*0.3, self.view.frame.size.height*0.16, self.view.frame.size.height*0.16)];
            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"showjianquanimage%ld",(long)indexPath.row]];
            [showView addSubview:imageView];
            
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [showView removeFromSuperview];
            });
            
            
            
            
        }
        
        
        
    }else{
        
        [[MdIAPManager sharedmdManage] pchasemdbyProductID:self.mdshopArr[indexPath.row][@"productid"] completion:^(BOOL success, NSError * _Nullable error) {
            
            if (success) {
                [[NSUserDefaults standardUserDefaults] setInteger:[[NSUserDefaults standardUserDefaults]integerForKey:@"mddiamond"]+[self.mdshopArr[indexPath.row][@"count"] intValue] forKey:@"mddiamond"];
                
                [[GlobalStatusView sharedInstance] updateDiamondCount:100];
                
            }
            
            
        }];
        
        
        
    }
    
}

@end
